<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Controllers\Controller;
use App\Http\Controllers\common\WechatController;

use DB;
class BindingController extends Controller{
    //首页
    public function getIndex(){
        return view('admin.binding.index');
    }
    public function getStore(Request $request)
    {
        $code = $request -> input('code','');
        $access = WechatController::codetojson($code);
        session(['openid'=>$access['openid']]);
        $user = DB::table('sa_member') -> select('uid','username') ->where('wechat_openid','=',$access['openid']) ->first();
        if(empty($user)){
            // 此用户未绑定
            return redirect('admin/binding');
        }else{
            return redirect('admin/binding/apply');
        }
    }

    //绑定用户处理
    public function postBm(Request $request){
        $phone = $request -> input('phone');
        $openid =session('openid');
        //修改用户表
        $row = DB::table('sa_member') -> where('mobile','=',$phone) -> update(['wechat_openid'=>$openid]);
        if($row){
            $data['status'] = '1';
            $data['msg'] = '绑定成功';
        }else{
            $data['status'] = '-1';
            $data['msg'] = '绑定失败';
        }
        echo json_encode($data);
    }

    public function getDele(Request $request)
    {
        $phone = $request -> input('phone');
        $openid =session('openid');
        //修改用户表
        $row = DB::table('sa_member') -> where('wechat_openid','=',$openid) -> update(['wechat_openid'=>Null]);
        if($row){
            $data['status'] = '1';
            $data['msg'] = '解绑成功';
            return redirect('admin/binding');
        }else{
            $data['status'] = '-1';
            $data['msg'] = '解绑失败';
            return redirect('admin/binding/apply');
        }
    }

    //绑定后选择应用页面
    public function getApply(){
        $openid = session('openid');
        $user = DB::table('sa_member') -> select('uid','username') ->where('wechat_openid','=',$openid) ->first();
        $det = ['smoke','electric','gas','call_help','infrared'];
        $data = [];
        foreach ($det as $v) {
            $$v =  DB::table('sa_'.$v.'_detector')
                ->whereIn('r_id',function ($query) use ($user,$openid){
                    $query -> select('mma_r_id')
                        ->from('sa_member_manager_area')
                        ->where(DB::raw("concat(',',mma_uid,',')"),'like',"%,".$user['uid'].",%");
                }) -> count();
            $data[$v] = $$v;
        }
        return view('/admin/binding/apply',['data'=>$data]);
    }

    //布撤防页面
    public function getDisarm(Request $request){
        //查询设备布撤防状态
        $apply = $request -> input('apply');
        $openid = session('openid');
        $user = DB::table('sa_member') -> select('uid','username') ->where('wechat_openid','=',$openid) ->first();
        $res = DB::table('sa_'.$apply.'_detector')
            ->whereIn('r_id',function ($query) use ($user,$openid){
                $query -> select('mma_r_id')
                    ->from('sa_member_manager_area')
                    ->where(DB::raw("concat(',',mma_uid,',')"),'like',"%,".$user['uid'].",%");
            }) -> first();
        return view('admin.binding.disarm',['res'=>$res,'apply'=>$apply]);
    }

    //布防撤防处理
    public function postDisarm(Request $request){
        $apply = $request -> input('apply');
        $openid = session('openid');
        $user = DB::table('sa_member') -> select('uid','username') ->where('wechat_openid','=',$openid) ->first();
        $status = $request -> input('status');
        if($status == 0){
            $res = DB::table('sa_'.$apply.'_detector')
                ->whereIn('r_id',function ($query) use ($user,$openid){
                    $query -> select('mma_r_id')
                        ->from('sa_member_manager_area')
                        ->where(DB::raw("concat(',',mma_uid,',')"),'like',"%,".$user['uid'].",%");
                }) -> update(['status'=>0]);
            if($res){
                return 0;
            }
        }else{
            $res = DB::table('sa_'.$apply.'_detector')
                ->whereIn('r_id',function ($query) use ($user,$openid){
                    $query -> select('mma_r_id')
                        ->from('sa_member_manager_area')
                        ->where(DB::raw("concat(',',mma_uid,',')"),'like',"%,".$user['uid'].",%");
                }) -> update(['status'=>1]);
            if($res){
                return 1;
            }
        }
    }
}